﻿define([
    'domReady!',
    'jquery-confirm',
    'bootstrap-table',
    'bootstrap-table-zh-CN',
    'bootstrap-select',
    'defaults-zh_CN',
    'lyear-loading',
    'validate',
    'validate_config',
    'jquery.ajaxRequest'
], function (domReady) {
    // table使用
    var $table = $('.table');
    //表单验证
    var v = $("#form").validate({
        rules: {
            name: {
                required: true
            },

        },
        messages: {
            name: {
                required: "角色名称不能为空"
            },

        }
    });
    function init() {

        //页面数据绑定
        dataBind();
    }

    //表格数据请求
    function dataBind(isRefresh = false) {
        if (isRefresh) {
            $('.page-loading').ajaxRequest("/BMS/Role/GetList", { name: "" }, function (data) { refreshTable(data.data) }, true, false);
        } else {

            $('.page-loading').ajaxRequest("/BMS/Role/GetList", { name: "" }, function (data) { bindTable(data.data) }, true, false);
        }
    }

    function refreshTable(data) {
        $table.bootstrapTable('load', data);
    }


    /**
     * 表格数据绑定
     * @param {any} data
     */
    function bindTable(data) {
        //表格控件初始化
        $table.bootstrapTable({
            data: data,
            idField: 'id',
            uniqueId: 'id',
            dataType: 'json',
            columns: [
                {
                    field: 'id',
                    title: 'ID'
                },
                {
                    field: 'name',
                    title: '名称',
                    width: 200
                },
                {
                    field: 'userCount',
                    title: '用户数'
                },
                {
                    field: 'status',
                    title: '状态',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            is_checked = '';
                        } else if (value == 0) {
                            is_checked = 'checked="checked"';
                        }
                        result = '<div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" id="customSwitch' + row.id + '" ' + is_checked + '><label class="custom-control-label role-switch" for="customSwitch' + row.id + '" ></label></div>';

                        return result;
                    },
                    events: {
                        'click .role-switch': function (e, value, row, index) {
                            updateStatus(row.id, value);
                        }
                    }
                },
                {
                    field: 'lastUser',
                    title: '操作人'
                },
                {
                    field: 'lastTime',
                    title: '操作时间'
                },
                {
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    width: 420,
                    events: {

                        'click .role-delete': function (e, value, row, index) {
                            del(row.id);
                        },
                        'click .role-edit': function (e, value, row, index) {
                            update(row);
                        },
                        'click .role-up': function (e, value, row, index) {
                            move(row.id, 1);
                        },
                        'click .role-down': function (e, value, row, index) {
                            move(row.id, 2);
                        },
                    },
                    formatter: operateFormatter
                }
            ],
        });
    }

    // 操作按钮
    function operateFormatter(value, row, index) {
        return [
            '<a type="button" class="role-setAuthority btn btn-xs btn-default m-r-5" title="设置权限" data-toggle="tooltip" href="/BMS/Role/RoleFunction/' + row.id + '"><i class="mdi mdi-key"></i>设置权限</a>',
            '<a type="button" class="role-setUser btn btn-xs btn-default m-r-5" title="设置用户" data-toggle="tooltip" href="/BMS/Role/UserRole/' + row.id + '"><i class="mdi mdi-account-group"></i>设置用户</a>',
            '<a type="button" class="role-edit btn btn-xs btn-default m-r-5" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i>编辑</a>',
            '<a type="button" class="role-up btn btn-xs btn-default m-r-5 ' + (row.isAllowUp ? '' : 'disabled') + '" title="上移" data-toggle="tooltip"><i class="mdi mdi-chevron-up-circle"></i>上移</a>',
            '<a type="button" class="role-down btn btn-xs btn-default m-r-5 ' + (row.isAllowDown ? '' : 'disabled') + '" title="下移" data-toggle="tooltip"><i class="mdi mdi-chevron-down-circle"></i>下移</a>',
            '<a type="button" class="role-delete btn btn-xs btn-default" title="删除" data-toggle="tooltip"><i class="mdi mdi-delete"></i>删除</a>'
        ].join('');
    }




    function add(id) {
        alert("add 方法 , id = " + id);
    }
    function move(id, way) {
        $(this).ajaxRequest("/BMS/Role/Move", { id: id, way: way }, function () { dataBind(true) },false);
        
    }
    function del(id) {
        $.confirm({
            title: '操作确认',
            content: '确定要删除吗？',
            escapeKey: 'cancelAction',
            buttons: {
                confirm: {
                    btnClass: 'btn-red',
                    text: '是',
                    keys: ['enter'],
                    action: function () {
                        $(this).ajaxRequest("/BMS/Role/Delete", { id: id }, function () { dataBind(true) },false);

                    }
                },
                cancelAction: {
                    text: '再想想',
                    action: function () {

                    }
                }
            }
        });
    }
    /**
     * 更改状态
     * @param {any} id
     * @param {any} state
     */
    function updateStatus(id, state) {
        var newstate = (state == 1) ? 0 : 1; // 发送参数值跟当前参数值相反
        $(this).ajaxRequest("/BMS/Role/UpdateStatus", { id: id, status: newstate }, function (data) { dataBind(true) },false)

    }

    /**
     * 列表编辑按钮事件
     * @param {any} row
     */
    function update(row) {
        $("#id").val(row.id);
        $("#name").val(row.name);

        $("#dialogModal").modal('show');
    }

    function bind() {
        //新增按钮事件
        $(document).on("click", "#btnAdd", function (event) {
            $("#id").val(0);
            $("#name").val('');

            $("#dialogModal").modal("show");
        })

        //模态框的保存事件
        $(document).on("click", "#btnSave", function () {
            if (!v.form()) {
                return;
            }
            $(this).attr('disabled', true);
            $this = $(this)
            var id = $("#id").val();
            var name = $("#name").val();
            $(this).ajaxRequest("/BMS/Role/Save", { id: id, name: name }, function (data) {
                dataBind(true)
                $("#dialogModal").modal("hide");
            }, true, false, function () { $this.attr('disabled', false);});
        })
        //模态框消失事件
        $("#dialogModal").on("hidden.bs.modal", function (e) {
            $("#btnSave").prop('disabled', false)
        })
    }

    $(document).ready(function () {
        init();
        bind();
    })

});